<?php
/**
 * User: Allen
 * Date: 15-12-04
 * 管理员中心控制器
 */

class Access extends PC_Controller{
	
	protected $go_url = '/admin/role/role_list';
	public function __construct(){
		parent::__construct();
		$this->load->model('access_model');
		
	}
	
	/**
	 * @brief 加载权限编辑页面
	 * @param Null
	 * @par 2015/12/04 Ver 1.00 Created by Allen
	 */
	public function edit_page() {
		$role_id = $this->uri->segment(4);
		$this->load->model('node_model');
		$nodes = $this->node_model->getNodeAll();//通过model获取所有节点,返回二维数组
		$nodes = $this->merge_node($nodes);//递归数组成为父子数组（多维数组）eg:array(1=>array('name'=>3,'child'=>array()))
		$this->data['nodes'] = $nodes;
		$node_info = $this->access_model->getAccessAll(array('role_id'=>$role_id));		
		$this->data['data'] = $node_info;
		$this->data['role_id'] = $role_id;
		$this->load->view('/admin/access_edit',$this->data);
	}

	/**
	 * @brief 提交权限编辑操作
	 * @param Null
	 * @par 2015/12/04 Ver 1.00 Created by Allen
	 */
	public function edit() {
		$data = $_POST;
		$where['role_id'] = $data['role_id'];
		$this->access_model->deleteAccess($where);
		$access = $data['node_id'];
		foreach ($access as $key => $value) {
			$add_data = array('role_id'=>$data['role_id'],'node_id'=>$value);
			$edit_result = $this->access_model->addAccess($add_data);
		}
		if($edit_result) {
			$this->location_href($this->go_url."/4");
		}else{
			$this->location_href($this->go_url."/5");
		}
	}
}